package com.txby;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import com.tencent.xinge.ClickAction;
import com.tencent.xinge.Message;
import com.tencent.xinge.MessageIOS;
import com.tencent.xinge.Style;
import com.tencent.xinge.TimeInterval;
import com.tencent.xinge.XingeApp;
import com.tencent.xinge.TagTokenPair;

public class Demo {
	private static XingeApp xinge;
	private static Message message1;
	private static Message message2;
	private static MessageIOS messageIOS;
	private static long accessId = 2100267181;
	private static String secretKey = "013214e38ee52edefdfff0d8631cb5ff";
	private static String title = "点餐提醒";
	private static String content = "尊敬的${name}患者：为了你的正常就餐,请在10前向工作人员预订。";
	private static String token = "e9f146a7321d79764bb2a4b82e691d0dfd2c0054";

	public static void main(String[] args) {
		/**
		 * 徐新凯测试
		 */
		/*
		 * XingeApp xinge = new XingeApp(accessId, secretKey);
		 * token="e9f146a7321d79764bb2a4b82e691d0dfd2c0054";
		 * System.out.println(xinge.queryInfoOfToken(token));
		 * System.out.println(XingeApp.pushTokenAndroid(accessId, secretKey,
		 * title, content, token));
		 */
		/**
		 * 我测试
		 */
		token = "98909e7bd41de2fc42dd9a638435520000983ccd";
		accessId = 2100267178;
		secretKey = "3068da41c501f821e70300851ad5ab40";
		xinge = new XingeApp(accessId, secretKey);
		/*
		 * 2100267178 ACCESS KEY AIUQ6138G4SK
		 */
		System.out.println(xinge.pushTokenAndroid(accessId, secretKey, title, content, token));
	 
		
		

		/*
		 * System.out.println(xinge.pushTokenAndroid(accessId, secretKey, title,
		 * content, token));
		 */

		// demoXingeSimple();

		/*
		 * Demo t = new Demo(); // Android单推
		 * System.out.println(t.demoPushSingleDeviceMessage());
		 * System.out.println(t.demoPushSingleDeviceNotification());
		 * System.out.println(t.demoPushSingleDeviceNotificationIntent());
		 * System.out.println(t.demoPushSingleAccount());
		 * 
		 * 
		 * 
		 * // Android批量推送 System.out.println(t.demoPushAccountList());
		 * System.out.println(t.demoPushAllDevice());
		 * System.out.println(t.demoPushTags());
		 * System.out.println(t.demoPushAccountListMultiple());
		 * System.out.println(t.demoPushDeviceListMultiple());
		 * 
		 * // 查询接口 System.out.println(t.demoQueryPushStatus());
		 * System.out.println(t.demoQueryDeviceCount());
		 * System.out.println(t.demoQueryTags());
		 * System.out.println(t.demoQueryTagTokenNum());
		 * System.out.println(t.demoQueryTokenTags());
		 * System.out.println(t.demoQueryInfoOfToken());
		 * System.out.println(t.demoQueryTokensOfAccount());
		 * 
		 * // 变更接口 System.out.println(t.demoCancelTimingPush());
		 * System.out.println(t.demoBatchSetTag());
		 * System.out.println(t.demoBatchDelTag());
		 * System.out.println(t.demoDeleteTokenOfAccount());
		 * System.out.println(t.demoDeleteAllTokensOfAccount());
		 */
	}

	/**
	 * 调用简易接口
	 */
	public static void demoXingeSimple() {
		// token="";
		// 易用接口
		System.out.println(XingeApp.pushTokenAndroid(accessId, secretKey, title, content,
				"98909e7bd41de2fc42dd9a638435520000983ccd"));

		/*
		 * System.out.println(XingeApp.pushTokenAndroid(000, "secretKey",
		 * "test", "测试", "token"));
		 * System.out.println(XingeApp.pushAccountAndroid(000, "secretKey",
		 * "test", "测试", "account"));
		 * System.out.println(XingeApp.pushAllAndroid(000, "secretKey", "test",
		 * "测试")); System.out.println(XingeApp.pushTagAndroid(000, "secretKey",
		 * "test", "测试", "tag"));
		 * 
		 * System.out.println(XingeApp.pushTokenIos(000L, "secretKey", "测试",
		 * "token", XingeApp.IOSENV_DEV));
		 * System.out.println(XingeApp.pushAccountIos(000L, "secretKey", "测试",
		 * "account", XingeApp.IOSENV_DEV));
		 * System.out.println(XingeApp.pushAllIos(000L, "secretKey", "测试",
		 * XingeApp.IOSENV_DEV)); System.out.println(XingeApp.pushTagIos(000L,
		 * "secretKey", "测试", "tag", XingeApp.IOSENV_DEV));
		 */
	}

	// 单个设备下发透传消息
	protected JSONObject demoPushSingleDeviceMessage() {
		Message message = new Message();
		message.setTitle("title");
		message.setContent("content");
		message.setType(Message.TYPE_MESSAGE);
		message.setExpireTime(86400);
		JSONObject ret = xinge.pushSingleDevice("token", message);

		return ret;
	}

	// 单个设备下发通知消息
	protected JSONObject demoPushSingleDeviceNotification() {
		JSONObject ret = xinge.pushSingleDevice("token", message1);
		return ret;
	}

	// 单个设备下发通知Intent
	// setIntent()的内容需要使用intent.toUri(Intent.URI_INTENT_SCHEME)方法来得到序列化后的Intent(自定义参数也包含在Intent内）
	// 终端收到后可通过intent.parseUri()来反序列化得到Intent
	protected JSONObject demoPushSingleDeviceNotificationIntent() {
		JSONObject ret = xinge.pushSingleDevice("token", message2);
		return ret;
	}

	// 下发单个账号
	protected JSONObject demoPushSingleAccount() {
		Message message = new Message();
		message.setExpireTime(86400);
		message.setTitle("title");
		message.setContent("content");
		message.setType(Message.TYPE_MESSAGE);
		JSONObject ret = xinge.pushSingleAccount(0, "joelliu", message);
		return ret;
	}

	// 下发多个账号
	protected JSONObject demoPushAccountList() {
		Message message = new Message();
		message.setExpireTime(86400);
		message.setTitle("title");
		message.setContent("content");
		message.setType(Message.TYPE_MESSAGE);
		List<String> accountList = new ArrayList<String>();
		accountList.add("joelliu");
		JSONObject ret = xinge.pushAccountList(0, accountList, message);
		return ret;
	}

	// 下发所有设备
	protected JSONObject demoPushAllDevice() {
		JSONObject ret = xinge.pushAllDevice(0, message1);
		return ret;
	}

	// 下发标签选中设备
	protected JSONObject demoPushTags() {
		List<String> tagList = new ArrayList<String>();
		tagList.add("joelliu");
		tagList.add("phone");
		JSONObject ret = xinge.pushTags(0, tagList, "OR", message1);
		return ret;
	}

	// 大批量下发给帐号 // iOS 请构造MessageIOS消息
	protected JSONObject demoPushAccountListMultiple() {
		Message message = new Message();
		message.setExpireTime(86400);
		message.setTitle("title");
		message.setContent("content");
		message.setType(Message.TYPE_NOTIFICATION);

		JSONObject ret = xinge.createMultipush(message);
		if (ret.getInt("ret_code") != 0)
			return ret;
		else {
			JSONObject result = new JSONObject();

			List<String> accountList1 = new ArrayList<String>();
			accountList1.add("joelliu1");
			accountList1.add("joelliu2");
			// ...
			result.append("all",
					xinge.pushAccountListMultiple(ret.getJSONObject("result").getLong("push_id"), accountList1));

			List<String> accountList2 = new ArrayList<String>();
			accountList2.add("joelliu3");
			accountList2.add("joelliu4");
			// ...
			result.append("all",
					xinge.pushAccountListMultiple(ret.getJSONObject("result").getLong("push_id"), accountList2));
			return result;
		}
	}

	// 大批量下发给设备 // iOS 请构造MessageIOS消息
	protected JSONObject demoPushDeviceListMultiple() {
		Message message = new Message();
		message.setExpireTime(86400);
		message.setTitle("title");
		message.setContent("content");
		message.setType(Message.TYPE_NOTIFICATION);

		JSONObject ret = xinge.createMultipush(message);
		if (ret.getInt("ret_code") != 0)
			return ret;
		else {
			JSONObject result = new JSONObject();

			List<String> deviceList1 = new ArrayList<String>();
			deviceList1.add("joelliu1");
			deviceList1.add("joelliu2");
			// ...
			result.append("all",
					xinge.pushDeviceListMultiple(ret.getJSONObject("result").getLong("push_id"), deviceList1));

			List<String> deviceList2 = new ArrayList<String>();
			deviceList2.add("joelliu3");
			deviceList2.add("joelliu4");
			// ...
			result.append("all",
					xinge.pushDeviceListMultiple(ret.getJSONObject("result").getLong("push_id"), deviceList2));
			return result;
		}
	}

	// 查询消息推送状态
	protected JSONObject demoQueryPushStatus() {
		List<String> pushIdList = new ArrayList<String>();
		pushIdList.add("390");
		pushIdList.add("389");
		JSONObject ret = xinge.queryPushStatus(pushIdList);
		return ret;
	}

	// 查询设备数量
	protected JSONObject demoQueryDeviceCount() {
		JSONObject ret = xinge.queryDeviceCount();
		return ret;
	}

	// 查询标签
	protected JSONObject demoQueryTags() {
		JSONObject ret = xinge.queryTags();
		return ret;
	}

	// 查询某个tag下token的数量
	protected JSONObject demoQueryTagTokenNum() {
		JSONObject ret = xinge.queryTagTokenNum("tag");
		return ret;
	}

	// 查询某个token的标签
	protected JSONObject demoQueryTokenTags() {
		JSONObject ret = xinge.queryTokenTags("token");
		return ret;
	}

	// 取消定时任务
	protected JSONObject demoCancelTimingPush() {
		JSONObject ret = xinge.cancelTimingPush("32");
		return ret;
	}

	// 设置标签
	protected JSONObject demoBatchSetTag() {
		List<TagTokenPair> pairs = new ArrayList<TagTokenPair>();

		// 切记把这里的示例tag和示例token修改为你的真实tag和真实token
		pairs.add(new TagTokenPair("tag1", "token00000000000000000000000000000000001"));
		pairs.add(new TagTokenPair("tag2", "token00000000000000000000000000000000001"));

		JSONObject ret = xinge.BatchSetTag(pairs);
		return ret;
	}

	// 删除标签
	protected JSONObject demoBatchDelTag() {
		List<TagTokenPair> pairs = new ArrayList<TagTokenPair>();

		// 切记把这里的示例tag和示例token修改为你的真实tag和真实token
		pairs.add(new TagTokenPair("tag1", "token00000000000000000000000000000000001"));
		pairs.add(new TagTokenPair("tag2", "token00000000000000000000000000000000001"));

		JSONObject ret = xinge.BatchDelTag(pairs);

		return ret;
	}

	// 查询某个token的信息
	protected JSONObject demoQueryInfoOfToken() {
		JSONObject ret = xinge.queryInfoOfToken("token");
		return ret;
	}

	// 查询某个account绑定的token
	protected JSONObject demoQueryTokensOfAccount() {
		JSONObject ret = xinge.queryTokensOfAccount("nickName");
		return ret;
	}

	// 删除某个account绑定的token
	protected JSONObject demoDeleteTokenOfAccount() {
		JSONObject ret = xinge.deleteTokenOfAccount("nickName", "token");
		return ret;
	}

	// 删除某个account绑定的所有token
	protected JSONObject demoDeleteAllTokensOfAccount() {
		JSONObject ret = xinge.deleteAllTokensOfAccount("nickName");
		return ret;
	}

	protected void buildMesssges() {
		message1 = new Message();
		message1.setType(Message.TYPE_NOTIFICATION);
		Style style = new Style(1);
		style = new Style(3, 1, 0, 1, 0);
		ClickAction action = new ClickAction();
		action.setActionType(ClickAction.TYPE_URL);
		action.setUrl("http://xg.qq.com");
		Map<String, Object> custom = new HashMap<String, Object>();
		custom.put("key1", "value1");
		custom.put("key2", 2);
		message1.setTitle("title");
		message1.setContent("大小");
		message1.setStyle(style);
		message1.setAction(action);
		message1.setCustom(custom);
		TimeInterval acceptTime1 = new TimeInterval(0, 0, 23, 59);
		message1.addAcceptTime(acceptTime1);

		message2 = new Message();
		message2.setType(Message.TYPE_NOTIFICATION);
		message2.setTitle("title");
		message2.setContent("通知点击执行Intent测试");
		style = new Style(1);
		action = new ClickAction();
		action.setActionType(ClickAction.TYPE_INTENT);
		action.setIntent("intent:10086#Intent;scheme=tel;action=android.intent.action.DIAL;S.key=value;end");
		message2.setStyle(style);
		message2.setAction(action);

	}

	public Demo() {
		XingeApp xinge = new XingeApp(accessId, secretKey);
		buildMesssges();
	}

}
